package com.fengwk.support.proxy.http;

import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fengwk.support.exception.BaseException;
import com.fengwk.support.util.ArrayUtils;
import com.fengwk.support.util.HttpClientUtils;
import com.fengwk.support.util.HttpClientUtils.ResponseEntity;

public class ConnectImpl implements Connect {

	@Override
	public Response connect(
			RequestMapping requestMapping, 
			PostMapping postMapping, 
			GetMapping getMapping, 
			Request request) {
		RequestMethod requestMethod = getRequestMethod(requestMapping, postMapping, getMapping);
		String url = request.getUrl();
		Map<String, String> params = request.getParams();
		ResponseEntity result = null;
		switch (requestMethod) {
		case GET:
			result = get(url, params);
			break;
		case HEAD:
			result = head(url, params);
			break;
		case POST:
			result = post(url, params);
			break;
		case PUT:
			result = put(url, params);
			break;
		case PATCH:
			result = patch(url, params);
			break;
		case DELETE:
			result = delete(url, params);
			break;
		case OPTIONS:
			result = options(url, params);
			break;
		case TRACE:
			result = trace(url, params);
			break;
		}
		Response response = new Response();
		response.setResponse(result.getStringContent());
		return response;
	}
	
	private RequestMethod getRequestMethod(RequestMapping requestMapping, PostMapping postMapping, GetMapping getMapping) {
		RequestMethod m = RequestMethod.POST;
		RequestMethod[] methods = null;
		if (requestMapping != null) {
			methods = requestMapping.method();
		} else if (postMapping != null) {
			m = RequestMethod.POST;
		} else if (getMapping != null) {
			m = RequestMethod.GET;
		}
		if (ArrayUtils.isNotBlank(methods)) {
			m = methods[0];
		}
		return m;
	}
	
	private ResponseEntity get(String url, Map<String, String> params) {
		return HttpClientUtils.get(url, params, null);
	}

	private ResponseEntity post(String url, Map<String, String> params) {
		return HttpClientUtils.post(url, params, null);
	}

	private ResponseEntity head(String url, Map<String, String> params) {
		throw new BaseException("can not support head");
	}

	private ResponseEntity put(String url, Map<String, String> params) {
		throw new BaseException("can not support put");
	}

	private ResponseEntity patch(String url, Map<String, String> params) {
		throw new BaseException("can not support patch");
	}

	private ResponseEntity delete(String url, Map<String, String> params) {
		throw new BaseException("can not support delete");
	}

	private ResponseEntity options(String url, Map<String, String> params) {
		throw new BaseException("can not support options");
	}

	private ResponseEntity trace(String url, Map<String, String> params) {
		throw new BaseException("can not support trace");
	}

}
